﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Codeplus.Dhruvtaata.Web.Models;
using Codeplus.Dhruvtaata.Web.Models.Entity;
using Codeplus.Dhruvtaata.Web.Models.EntityAction;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MiniProfiler.Windows;
using StackExchange.Profiling;

namespace UnitTests {

    [TestClass]
    public class QAFixture {

        [TestInitialize]
        public void Setup() {
            MiniProfilerEF.Initialize();
            ConsoleProfiling.Start();
        }

        [TestMethod]
        public void ShouldAddANewQuestionWithChoicesAndCorrectAnswer() {
            using (var dataContext = new DatabaseContext()) {
                var subject = new Subject() {
                    SubjectId = 3,
                    Name = "Reasoning",
                    ParentSubjectId = null
                };
                dataContext.Subjects.Add(subject);
                var questionAction = new QuestionCrudAction();
                questionAction.SetDataContext(dataContext);
                questionAction.Create(new Question() {
                    Text = "What is a noun?",
                    Choices = new List<string>() { "its a name of a person, place or thing", "its a form of salt", "its used in Hindi language", "its an action performed by a person or thing" },
                    CorrectAnswer = 1,
                    Level = 1,
                    Subject = subject
                });
                var message = ConsoleProfiling.StopAndGetConsoleFriendlyOutputStringWithSqlTimings();
                Debug.WriteLine(message);
                dataContext.SaveChanges();

                //Debug.WriteLine(MiniProfiler.ToJson(QAFixture.Current));
                var result = questionAction.Retrieve(q => q.Text == "What is a noun?", q => q.QuestionId, 0, 10);
                Assert.AreEqual(1, result.Count());

                //dataContext.SaveChanges();
            }
        }
    }
}